﻿using PC.PickleJar.Attributes;
using PC.PickleJar.Configuration;
using PC.PickleJar.DataProviders;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PC.PickleJar.Validators
{
	public abstract class ValidatorBase
	{
		public ValidatorBase(IDataProvider dataProvider)
		{
			this.DataProvider = dataProvider;
		}

		public virtual ValidationResult Validate(SourceItemAttribute item, DateTime timestamp)
		{
			ValidationResult result = new ValidationResult();

			var sourceItem = this.DataProvider.GetSourceItem(item.ItemKey);

			TimeSpan gap = new TimeSpan(sourceItem.Timestamp.Ticks - timestamp.Ticks);

			if (((int)gap.TotalSeconds) > 0)
			{
				result.RequiresAttention = true;
				result.Reason =
					String.Format(
						CultureInfo.InvariantCulture,
						"{0}{3}{3}{1}{3}{3}is stale by {2:0.00} days.{3}Related tests should be re-validated.",
						sourceItem.FriendlyTypeName,
						sourceItem.ItemKey,
						gap.TotalDays,
						Environment.NewLine);
			}

			return result;
		}

		protected IDataProvider DataProvider
		{
			get;
			private set;
		}

		public abstract IEnumerable<Type> TargetAttributeTypes
		{
			get;
			protected set;
		}
	}
}
